<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery.min.js"></script>
    <script src="p5.js"></script>
    <script src="p5.play.js"></script>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        #leftbox {
            width: 20%;
            height: calc(100vh);
            box-sizing: border-box;
            border-right: 1px solid #ccc;
            float: left;
        }
        #rightbox {
            width: 80%;
            height: calc(100vh);
            float: left;            
        }
        .toolbtn{
            display: block;
            padding: 2px;
            text-align: center;
            background-color: #0273DD;
            margin: 10px;
            color:white;
        }
    </style>
</head>
<body>
    <div id="leftbox">
        <a class="toolbtn" href="#"  tool-id="1">矩形</a>
    </div>
    <div id="rightbox"></div>
    <script src="help.js"></script>
    <script>
        
        var drawobjs = [];
        var r1;
        function setup() {
            var drawbox = document.getElementById("rightbox");            
            var myCanvas = createCanvas(drawbox.clientWidth, drawbox.clientHeight);
            myCanvas.parent('rightbox');
            r1 = p5help.create_rect(200,200,20,20);         
             
        }
        
        function draw(){
            background(230);           
            if (mouseIsPressed && curtoolfunc!=null){
                var stretchy = curtoolfunc();
                stretchy.position.x = mouseX;
                stretchy.position.y = mouseY;
                curtoolfunc = null;
                drawobjs.push(stretchy);                
            }
            drawobjs.forEach(d=>{if(d.myismove){
                d.position.x = mouseX;
                d.position.y = mouseY;
            }});
            p5help.update();
            drawSprites();
            stroke(255, 0,0);
            rect(0,0,400,400);
            line(0,0,400,400);           
        }
        var curtoolfunc = null;
        $(function(){
            
            $(".toolbtn").click(function(){
                curtoolfunc = function(){
                    var stretchy = createSprite(400, 200, 50, 50);
                    stretchy.myR = 220;
                    stretchy.myG = 53;
                    stretchy.myB = 69;
                    stretchy.myW = 100;
                    stretchy.myH = 100;
                    stretchy.draw = function() {
                            fill(this.myR, this.myG, this.myB);
                            push();
                            //rotate(radians(this.getDirection()));
                            rect(0, 0, this.myW, this.myH);
                            pop();
                    };
                    stretchy.onMousePressed = function(){                        
                        if(stretchy.myismove != true){
                            stretchy.myismove = true;
                            stretchy.myR = 255;
                            stretchy.myG = 1;
                            stretchy.myB = 2;
                            //stretchy.rotation-= 10;
                            console.log(stretchy);
                        }  
                    };
                    stretchy.onMouseReleased = function(){                        
                        if(stretchy.myismove != false){
                            stretchy.myismove = false;
                            stretchy.myR = 220;
                            stretchy.myG = 53;
                            stretchy.myB = 69;
                            //stretchy.myW = stretchy.myW + 10;
                        } 
                    };
                    return stretchy                   
                }
            });
        });
    </script>
</body>
</html>